package cn.kgc.crowdfundingadmin.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

@Configuration//表示该类的配置类,等同于配置文件
@EnableWebSecurity//启动security
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MySecutityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权
        http.authorizeRequests()//对请求进行授权
                .antMatchers("/static/**","/index")//使用ANT风格设置要授权的URL地址
                .permitAll()//允许上面使用ANT风格设置的全部请求
              /*  .antMatchers("/admin/query/for/search")
                .hasAnyAuthority("user:get")*/
                .anyRequest()//其他未设置的全部请求
                .authenticated()//需要认证
                .and()
                .formLogin()//设置未授权请求跳转到登录页面
                .loginPage("/admin/to/login/page.html")//指定登录页
                .permitAll()//为登录页设置所有人都可以访问
                .loginProcessingUrl("/admin/secutity/do/login")//指定登录处理地址，不再使用原来的控制器
                .usernameParameter("loginAcct")//指定页面参数
                .passwordParameter("userPswd")
                .defaultSuccessUrl("/admin/to/main/page.html")//登录成功页面
                .failureUrl("/admin/to/login/page.html?error=true")//登录失败跳转URL，并传参方便前端进行判断
                .and()
                .logout()//启动退出功能
                .logoutUrl("/admin/security/logout")//指定退出处理地址，不再使用原来的控制器
                .logoutSuccessUrl("/index")
                .and()
                .csrf()
                .disable() // 禁用CSRF功能，如果不禁用，只能发post请求退出，否则报错
                .exceptionHandling().accessDeniedPage("/403")
    ;
                //记住我
               http.rememberMe().rememberMeParameter("remember");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //认证
       /* auth.inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("test")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("ren");*/

        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
